﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;

namespace CarInfor.UI
{
    public partial class frmInsuranceItemSelect : Form
    {
        public frmInsuranceItemSelect()
        {
            InitializeComponent();
        }

        private void frmInsuranceItemSelect_Load(object sender, EventArgs e)
        {
            Bind();
        }

        List<InsuranceItem> itemList = new List<InsuranceItem>();

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void Bind()
        {
            this.checkedListBox1.Items.Clear();
            InsuranceItem insuranceItem = new InsuranceItem();
            DataTable dt = insuranceItem.Query();
            foreach (DataRow row in dt.Rows)
            {
                this.checkedListBox1.Items.Add(row["险种名称"].ToString() + "(保额:" + decimal.Parse(row["保额"].ToString()).ToString("0.00") + ";金额:" + decimal.Parse(row["金额"].ToString()).ToString("0.00") + ")");
                itemList.Add(new InsuranceItem { ItemName = row["险种名称"].ToString(), InsuredAmount = decimal.Parse(row["保额"].ToString()), Price = decimal.Parse(row["金额"].ToString()) });
            }
        }

        /// <summary>
        /// 获取选择的险种
        /// </summary>
        /// <returns></returns>
        public List<InsuranceItem> GetSelectedItem()
        {
            List<InsuranceItem> lst = new List<InsuranceItem>();
            for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
            {
                if (this.checkedListBox1.GetItemChecked(i))
                {
                    lst.Add(itemList[i]);
                }
            }
            return lst;
        }

        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
            {
                this.checkedListBox1.SetItemChecked(i, true);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnColse_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
